<template>
  <div>
    <el-tabs>
      <el-tab-pane label="修改密码">
        <el-form ref="passwordForm" label-width="100px" :rules="passwordFormRules" :model="passwordForm">
          <el-form-item label="旧密码" prop="oldPassword">
            <el-input v-model="passwordForm.oldPassword" type="password" placeholder="请输入旧密码" />
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword">
            <el-input v-model="passwordForm.newPassword" type="password" placeholder="请输入新密码" />
          </el-form-item>
          <el-form-item label="确认新密码" prop="newPasswordConfirmation">
            <el-input v-model="passwordForm.newPasswordConfirmation" type="password" placeholder="请输入再次输入新密码" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSave">保存</el-button>
            <el-button type="danger" @click="handleClear">重新输入</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { update } from '../api/password'
export default {
  data() {
    const checkOldPassword = (rule, value, callback) => {
      if (value === this.passwordForm.newPassword) {
        callback(new Error('新密码不能和旧密码相同!'))
      } else {
        callback()
      }
    }
    const checkNewPassword = (rule, value, callback) => {
      if (value.length < 6 || value.length > 16) {
        callback(new Error('密码长度不能小于6位或大于16位!'))
      } else {
        if (value === this.passwordForm.oldPassword) {
          callback(new Error('新密码不能和旧密码相同!'))
        } else {
          callback()
        }
      }
    }
    const checkNewPasswordConfirmation = (rule, value, callback) => {
      if (value !== this.passwordForm.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      passwordForm: {
        oldPassword: '',
        newPassword: '',
        newPasswordConfirmation: ''
      },
      passwordFormRules: {
        oldPassword: [
          { required: true, message: '请输入旧密码', trigger: 'change' },
          { validator: checkOldPassword, trigger: 'change' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'change' },
          { validator: checkNewPassword, trigger: 'change' }
        ],
        newPasswordConfirmation: [
          { required: true, message: '请再次输入新密码', trigger: 'change' },
          { validator: checkNewPasswordConfirmation, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    handleSave() {
      this.$refs['passwordForm'].validate((valid) => {
        if (valid) {
          this.$confirm('您正在修改密码，确认继续吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            customClass: 'confirmDialog',
            center: true,
            type: 'warning'
          }).then(() => {
            update({
              old_password: this.passwordForm.oldPassword,
              new_password: this.passwordForm.newPassword,
              new_password_confirmation: this.passwordForm.newPasswordConfirmation
            }).then(res => {
              this.$message.success('密码修改成功，下次请使用新密码登录')
            }).catch((err) => {
              if (err.data) {
                if (err.data.message) {
                  this.$message.error(err.data.message)
                }
              }
              console.log(err)
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消操作'
            })
          })
        } else {
          this.$message.error('填写有误，请检查')
        }
      })
    },
    handleClear() {
      this.$refs['passwordForm'].resetFields()
      this.$message.success('输入已清空')
    }
  }
}
</script>

<style>
  .confirmDialog {
    width: 60%;
  }
</style>
